package com.truongnha.clienttn.data.dao;

import java.util.ArrayList;
import java.util.List;

import com.truongnha.clienttn.data.entity.StudentEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class StudentDAO {
	private final SQLiteDatabase db;

	public StudentDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert(String phone, String email, String sms_phone,
			String first_name, String last_name, String birthday,
			int student_id, String sex) {
		ContentValues inititalValue = new ContentValues();

		inititalValue.put(StudentScheme.PHONE, phone);
		inititalValue.put(StudentScheme.EMAIL, phone);
		inititalValue.put(StudentScheme.SMS_PHONE, sms_phone);
		inititalValue.put(StudentScheme.FIRST_NAME, first_name);
		inititalValue.put(StudentScheme.LAST_NAME, last_name);
		inititalValue.put(StudentScheme.BIRTHDAY, birthday);
		inititalValue.put(StudentScheme.STUDENT_ID, student_id);
		inititalValue.put(StudentScheme.SEX, sex);
		return db.insert(StudentScheme.TABLE_NAME, null, inititalValue);
	}

	public void delete() {
		db.delete(StudentScheme.TABLE_NAME, null, null);
	}

	public List<StudentEntity> getStudentByClass(int classId) {
		Log.d("LOG", "step 3.3.1");
		String sql = "SELECT  DISTINCT * from Student INNER JOIN  class_student on Student.student_id = class_student.student_id ";
		Cursor mcursor = db.rawQuery(sql, null);
		// AccountEntity = accountEntity()
		Log.d("LOG", "step 3.3.2");
		Log.d("LOG", mcursor.toString());

		mcursor.moveToFirst();

		List<StudentEntity> list = new ArrayList<StudentEntity>();
		while (!mcursor.isAfterLast()) {
//			StudentEntity studentEntity = new StudentEntity(mcursor.getInt(0),
//					mcursor.getString(1), mcursor.getString(2),
//					mcursor.getString(3), mcursor.getString(4),
//					mcursor.getString(5), mcursor.getString(6),
//					mcursor.getInt(7), mcursor.getString(8), mcursor.getInt(9));

			Log.d("log error",mcursor.getInt(0) + 
					mcursor.getString(1) + mcursor.getString(2));
//					mcursor.getString(3), mcursor.getString(4),
//					mcursor.getString(5), mcursor.getString(6),
//					mcursor.getInt(7), mcursor.getString(8), mcursor.getInt(9));
			//list.add(studentEntity);
			mcursor.moveToNext();
		}
		mcursor.close();
		return list;
	}

}
